package com.brave.mvvm.mvvmhelper.utils

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.annotation.ColorInt
import com.brave.mvvm.mvvmhelper.utils.generate.DrawableGenerate
import com.jakewharton.rxbinding4.view.clicks
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Consumer
import java.util.concurrent.TimeUnit

/**
 * ***author*** ：brave tou
 *
 * ***data***   : 2021/9/1 14:52
 *
 * ***desc***   : View工具
 */

/**
 * View是否显示
 */
fun View?.isVisible(): Boolean {
    if (null == this) return false
    return visibility == View.VISIBLE
}

/**
 * View不占位隐藏
 */
fun View?.gone() {
    if (null == this) return
    if (this.visibility != View.GONE) {
        this.visibility = View.GONE
    }
}

/**
 * View显示
 */
fun View?.visible() {
    if (null == this) return
    if (this.visibility != View.VISIBLE) {
        this.visibility = View.VISIBLE
    }
}

/**
 * View占位隐藏
 */
fun View?.invisible() {
    if (null == this) return
    if (this.visibility != View.INVISIBLE) {
        this.visibility = View.INVISIBLE
    }
}

/**
 * 取消焦点
 */
@JvmOverloads
fun View?.cancelNextFocus(
    left: Boolean = true,
    up: Boolean = true,
    right: Boolean = true,
    down: Boolean = true,
) {
    if (null == this) return
    if (left) this.nextFocusLeftId = this.id
    if (up) this.nextFocusUpId = this.id
    if (right) this.nextFocusRightId = this.id
    if (down) this.nextFocusDownId = this.id
}

/**
 * 打开焦点
 */
fun View?.openFocusable() {
    if (null == this) return
    this.isFocusable = true
    this.isFocusableInTouchMode = true
}

/**
 * 关闭焦点
 */
fun View?.closeFocusable() {
    if (null == this) return
    this.isFocusable = false
    this.isFocusableInTouchMode = false
}

/**
 * 打开点击
 */
fun View?.openEnabled() {
    if (null == this) return
    isEnabled = true
}

/**
 * 关闭点击
 */
fun View?.closeEnabled() {
    if (null == this) return
    isEnabled = false
}

/**
 * 打开选中
 */
fun View?.openSelected() {
    if (null == this) return
    isSelected = true
}

/**
 * 关闭选中
 */
fun View?.closeSelected() {
    if (null == this) return
    isSelected = false
}

/**
 * 设置View的宽高
 */
@JvmOverloads
fun View?.setSize(width: Int? = null, height: Int? = null) {
    if (null == this) return
    val lp = layoutParams
    when (lp) {
        is LinearLayout.LayoutParams -> {
            width?.let { lp.width = it }
            height?.let { lp.height = it }
        }
        is RelativeLayout.LayoutParams -> {
            width?.let { lp.width = it }
            height?.let { lp.height = it }
        }
        is FrameLayout.LayoutParams -> {
            width?.let { lp.width = it }
            height?.let { lp.height = it }
        }
        else -> {
            width?.let { lp.width = it }
            height?.let { lp.height = it }
        }
    }
    layoutParams = lp
}

/**
 * 设置View的外边距
 */
@JvmOverloads
fun View?.setMargin(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
    if (null == this) return
    val lp = layoutParams
    when (lp) {
        is LinearLayout.LayoutParams -> {
            left?.let { lp.leftMargin = it }
            top?.let { lp.topMargin = it }
            right?.let { lp.rightMargin = it }
            bottom?.let { lp.bottomMargin = it }
        }
        is RelativeLayout.LayoutParams -> {
            left?.let { lp.leftMargin = it }
            top?.let { lp.topMargin = it }
            right?.let { lp.rightMargin = it }
            bottom?.let { lp.bottomMargin = it }
        }
        is FrameLayout.LayoutParams -> {
            left?.let { lp.leftMargin = it }
            top?.let { lp.topMargin = it }
            right?.let { lp.rightMargin = it }
            bottom?.let { lp.bottomMargin = it }
        }
        is ViewGroup.MarginLayoutParams -> {
            left?.let { lp.leftMargin = it }
            top?.let { lp.topMargin = it }
            right?.let { lp.rightMargin = it }
            bottom?.let { lp.bottomMargin = it }
        }
    }
    layoutParams = lp
}

/**
 * 设置View的外边距
 */
fun View?.setMargin(size: Int) {
    setMargin(size, size, size, size)
}

/**
 * 设置View的内边距
 */
@JvmOverloads
fun View?.setPadding(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
    if (null == this) return
    val mPaddingLeft = left ?: paddingLeft
    val mPaddingTop = top ?: paddingLeft
    val mPaddingRight = right ?: paddingLeft
    val mPaddingBottom = bottom ?: paddingLeft
    this.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom)
}

/**
 * 设置View的内边距
 */
fun View?.setPadding(size: Int) {
    setPadding(size, size, size, size)
}

/**
 * View防止重复点击事件
 * @param duration 防止多次点击间隔时间（ms）
 * @param consumer 消费者
 */
@JvmOverloads
fun View.preventRepeatedClicks(
    consumer: Consumer<Any>?,
    duration: Long = 1000L,
): Disposable {
    return if (null == consumer) {
        this.clicks()
            .throttleFirst(duration, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {}
    } else {
        this.clicks()
            .throttleFirst(duration, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(consumer)
    }
}

/**
 * View背景绘制
 */
fun View?.drawBackdrop(): DrawableGenerate {
    return DrawableGenerate.with(this)
}

/**
 * View图片颜色渲染（View为ImageView默认渲染位图，其他则默认渲染背景）
 * @param color 渲染为指定颜色
 */
@JvmOverloads
fun View?.drawableTint(@ColorInt color: Int = Color.WHITE) {
    if (null == this) return
    val mDrawable =
        if (this is ImageView) {
            this.drawable ?: this.background
        } else {
            this.background
        } ?: return
    mDrawable.dyeing(color)
}